<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate"> 新增 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                placement: 'leftBottom',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <ExportComponentDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';

  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import ExportComponentDrawer from './ExportComponentDrawer.vue';
  import { columns } from './exportComponent.data';
  import { getVisualListExportTemplate, getVisualDelExport } from '/@/api/module/module';
  export default defineComponent({
    name: 'ExportComponent',
    components: { BasicTable, ExportComponentDrawer, TableAction },
    props: {
      formCode: {
        type: String,
        default: '',
      },
      moduleCode: {
        type: String,
        default: '',
      },
    },
    setup(props) {
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, setLoading }] = useTable({
        title: '',
        api: getVisualListExportTemplate,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: [],
          autoSubmitOnEnter: true,
        },
        // searchInfo: {
        //   op: 'search',
        // }, //额外的参数
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        pagination: false, //无分页
        beforeFetch: (info) => {
          return {
            ...info,
            moduleCode: props.moduleCode,
          };
        },
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
        });
      }

      async function handleDelete(record: Recordable) {
        setLoading(true);
        await getVisualDelExport({ id: record.id })
          .then(() => {
            handleSuccess();
          })
          .finally(() => {
            setLoading(false);
          });
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
      };
    },
  });
</script>
